package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IHomeService;
import com.github.pagehelper.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/20 20:28
 **/
@Service
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    ProductCateConverter productCateConverter;

    @Override
    public List<PanelDto> homepage() {
        //查询所有panel数据
        Example example = new Example(Panel.class);
        example.or().andEqualTo("status",GlobalConstants.PANEL_STATUS_AVA)
                    .andNotEqualTo("id",GlobalConstants.RECOMMEND_PANEL_ID);
        List<Panel> panels = panelMapper.selectByExample(example);
        for (Panel panel : panels) {
            example = new Example(PanelContent.class);
            example.or().andEqualTo("panelId",panel.getId());
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            List<PanelContentItem> panelContentItems = contentConverter.panelContentsDto(panelContents);
            for (PanelContentItem panelContentItem : panelContentItems) {
                if (panelContentItem.getProductId() != null) {
                    Item item = itemMapper.selectByPrimaryKey(panelContentItem.getProductId());
                    panelContentItem.setSalePrice(item.getPrice());
                    panelContentItem.setSubTitle(item.getSell_point());
                    panelContentItem.setProductName(item.getTitle());
                }
            }
            panel.setPanelContentItems(panelContentItems);
        }
        return contentConverter.panels2Dto(panels);
    }

    @Override
    public List<PanelContentDto> navigation() {
        Example example = new Example(PanelContent.class);
        example.or().andEqualTo("panelId",GlobalConstants.HEADER_PANEL_ID);
        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
        return contentConverter.panelContents2Dto(panelContents);
    }

    @Override
    public List<ProductCateDto> categories() {
        Example example = new Example(ItemCat.class);
        example.setOrderByClause(GlobalConstants.DEFAULT_SORT_COLLATION);
        List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
        return productCateConverter.items2Dto(itemCats);
    }

    @Override
    public PanelDto recommend() {
        Panel panel = panelMapper.selectByPrimaryKey(GlobalConstants.RECOMMEND_PANEL_ID);
        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
        panel.setPanelContentItems(panelContentItems);
        return contentConverter.panel2Dto(panel);
    }
}
